<template>
	<view :data-theme="theme" class="container">
		<view class="card">
			<view style="flex-flow: row;display: flex;">
				<view style="flex: 1;">
					<u--image :radius="5" :showLoading="true" :src="activeData.image" width="80px"
						height="80px"></u--image>
				</view>
				<view style="flex: 4;">
					<view style="display: flex;flex-flow: column;flex: 1;margin: 6px 6px 0 6px;">
						<view style="display: flex;align-items: center;position: relative;">
							<text class="u-line-1"
								style="font-size: 16px;font-weight: bold;margin: 20rpx 0;">{{activeData.name}}</text>
							<view style="font-size: 16px;font-weight: bold;margin-left: 10rpx;">{{activeData.sku}}
							</view>
						</view>

						<view class="name-param">
							<text
								style="font-size: 14px;color: #999;margin-right: 10px;">{{activeData.operateCityName}}</text>
							<view><text style="font-size: 28rpx;margin: 0 12rpx;">￥{{activeData.price}}</text><text
									style="text-decoration: line-through;color: #999;margin-left: 20rpx;">￥{{activeData.otPrice}}</text>
							</view>
						</view>
						<view class="name-param1">
							<text style="font-size: 14px;color: #999;margin-right: 10px;">商品分类</text>
							<text>{{activeData.categoryName}}</text>
						</view>
						<view class="name-param1">
							<text style="font-size: 14px;color: #999;margin-right: 10px;">活动库存</text>
							<text>{{activeData.stock}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view style="margin: 30rpx;display: flex;justify-content: space-between;">
			<view style="font-weight: bold;font-size: 33rpx;">
				活动库存配置
			</view>
			<!-- <view class="main_color" style="font-size: 33rpx;" @click="batchSetup">
				<span>{{this.isShowCheckbox?'取消':'批量设置'}}</span>
			</view> -->
		</view>

		<scroll-view scroll-y="true" :style="{ 'margin-bottom':'140rpx' }" style="height: 100%; overflow: hidden;">
			<view class="scolling-page" v-for="(item,index) in depotData" :key="index">
				<view class="card">
					<view class="product_demo" style="align-items: center;">
						<!-- <u--image :radius="5" :showLoading="true" :src="item.image" width="80px" height="80px"></u--image> -->
						<view class="header_name" style="margin-left: 20rpx;">{{item.depotName}}</view>
					</view>
					<view class="content"
						style="display: flex;font-size: 30rpx;margin: 30rpx 0;margin-left: 20rpx;color: #999;">
						<view style="flex: 2;">
							仓库库存
						</view>
						<view style="flex: 3;">
							{{item.stock}}
						</view>
					</view>
					<view class="content"
						style="display: flex;font-size: 30rpx;margin: 30rpx 0;margin-left: 20rpx;color: #999;">
						<view style="flex: 2;">
							秒杀剩余库存
						</view>
						<view style="flex: 3;">
							{{item.remain ? item.remain : '0'}}
						</view>
					</view>
					<view class="card-bg"
						style="border-radius: 20rpx;background-color: #fafafa;padding: 20rpx;color: #636363;">
						<view class="content"
							style="display: flex;font-size: 30rpx;margin: 40rpx 0;align-items: center;">
							<view style="flex: 2;">
								活动库存
							</view>
							<view style="flex: 3;display: flex;align-items: center;">
								<view style="flex: 1;line-height: 60rpx;">
									<input type="number" step="0.01" v-model.trim="item.totalInventory" :min="0"
										maxlength="6"
										oninput="if(value<=0)value=0;if(value>=24)value=24;value=parseInt(value)"
										style="border: 2rpx solid #e3e3e3;background-color: #fefefe;width: 180rpx;height: 60rpx; border-radius: 10rpx;padding-left: 20rpx;" />
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="footer" v-if="!isShowCheckbox"
			style="position: fixed;bottom: 0px;width: 100%;padding: 10px;background-color: #FFF;align-items: center;">
			<view style="display: flex;align-items: center;">
				<button :color="bgColor" :loading="loading"
					style="font-weight: bold;border-radius: 10px;color: #000;padding: 20rpx;flex: 1;margin: 0 10rpx;background-color: #f2f2f2;"
					@click="goBackConsign()">取消</button>
				<button :color="bgColor" class="bg_color"
					style="font-weight: bold;border-radius: 10px;color: #FFF;padding: 20rpx;flex: 1;margin: 0 10rpx;"
					@click="addStockQuery()">提交设置</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		addSeckillProApi,
		stockQuerySeckillApi,
		addStockQueryApi
	} from "@/api/marketing.js";
	// import addedActiveGoods from "../components/added-active-goods.vue";
	// import tuiDrawer from '../components/tui-drawer/index.vue';
	import setSeckillPage from '../components/set-seckill-page.vue';
	import {
		setThemeColor
	} from '@/utils/setTheme.js';
	let app = getApp();
	export default {
		components: {
			setSeckillPage,
			// 	tuiDrawer
		},
		data() {
			return {
				bgColor: "#ffffff",
				theme: app.globalData.theme,
				productAttrParam: '',
				productData: [],
				activeDateTime: "",
				showUpClassPopup: false,
				activeData: {
					oldPrice: 13.92, //原价
					image: 'http://192.168.1.29:28080/img_xyt/public/product/2024/04/23/3669c2389d38458582e36ee84d6fe235o80gc46lgn.png',
					brandName: '绿色和美',
					name: "鸭血 6kg/箱(20盒)",
					status: 1, //活动状态
					datailContent: "提报价格需低于招商指导价，相同商品取提报活动价低品优先展示，给出价格力度越高，提报成功率越高。",
					endTime: "2024.4.30 23:59:59",
					activeTime: "2024.04.17-2024.04.30",
					queryStatus: 1, //1直接生效
					requirements: '冻品尝鲜价专栏(冻肉、速食熟食、冷冻半成品、调理包)，限商品新客可享尝鲜价优惠价格，通过商城流量及首页固定栏目位高效转化新客!',
				},
				totalInventory: null, //尝鲜促销价
				newPrice: null,
				showNewTop: false,
				//-------------------
				isAllChecked: false,
				isShowCheckbox: false, //批量设置
				// ---------弹窗------------
				showGroupCategory: false,
				// ------促销总库存模式--------
				skuArr2: [{
					name: "不限",
					type: '-1'
				}, {
					name: "自定义",
					type: '2'
				}],
				invoteKeyValue: {
					totalInventoryModel: '-1', //促销总库存
					promotionInventory: 1, //促销总库存初始值1
					daylimitModel: '-1', //单日限购状态
					customerTotalLimit: 1, //单日限购初始值1
					totalRestriction: '-1', //单客户总限购
					customerTotalLimit: 1, //单客户总限购初始值1
					showTotalInventory: false, //促销总库存自定义
					//-------------------------------------
					showDayLimitModel: false, //客户单日限购自定义
					//-------------------------------------
					showtotalRestriction: false, //客户总限购自定义
				},

				//------------------
				allSelect: [0],
				uniqueKey: 0,
				activityId: '',
				loading: false,
				checkboxValue: [],
				form: {
					id: 0,
				},
				depotData: [],
			};
		},
		onLoad(options) {
			this.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.bgColor,
			});

			let that = this;
			// this.activityId = options.activityId;
			that.activeData = JSON.parse(options.data)
			console.log(that.activeData);
			// // APP-NVUE平台暂不支持以this.getOpenerEventChannel()方式获取eventChannel，请换用this.$scope.eventChannel来获取
			// // #ifdef APP-NVUE
			// const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
			// // #endif
			// // #ifndef APP-NVUE
			// const eventChannel = this.getOpenerEventChannel();
			// // #endif
			// // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			// eventChannel.on('productAttr', function(param) {
			// 	that.productAttrParam = param;
			// 	console.log(param)
			// 	that.productData = param.data;
			// 	that.activeDateTime = param.activeDateTime;
			// });
			this.stockQuerySeckill();
		},
		methods: {
			stockQuerySeckill() {
				let skuId = this.activeData.skuId;
				stockQuerySeckillApi(skuId).then((res) => {
					console.log(res);
					this.depotData = res.data;
				})
			},
			/** 设置秒杀商品库存 */
			addStockQuery() {
				let addValueData = [];
				this.depotData.map((item) => {
					return addValueData.push({
						depotId: item.depotId,
						merId: item.merId,
						sku: item.sku,
						totalInventory: item.totalInventory
					})
				});
				addStockQueryApi(addValueData).then((res) => {
					console.log('res,', res)
					uni.showToast({
						title: "提交成功",
						icon: "success",
					});
					this.$router.push({
						path: `/seckillDemo/seckillDetail/index`
					});
				}).catch((err) => {
					console.log('err:', err)
				})
			},

			goPage(url, success) {
				uni.navigateTo({
					url,
					success: success
				})
			},

			// 批量设置
			batchSetup() {
				this.isShowCheckbox = !this.isShowCheckbox;
			},

			// 返回上一级，选择商品
			goBackConsign() {
				let pages = getCurrentPages() // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
				let prevPage = pages[pages.length - 2] //上一页页面实例
				uni.navigateBack({
					delta: 1,
					// success: () => {
					// 	prevPage.$vm.otherFun(param) // 给上一页绑定方法otherFun,
					// }
				})
			},

			/** 确认 */
			submitForm() {
				console.log(this.productData)
				for (let ele of this.productData) {
					if (ele.activityPrice == '' || !ele.activityPrice) {
						uni.showToast({
							title: "请先设置商品价格",
							icon: "none",
						})
						return
					};
					if (ele.quota == '0' || !ele.quota) {
						uni.showToast({
							title: "库存应当大于0",
							icon: "none",
						})
						return
					}
				}

				let newDataValue = this.productData.reduce((acc, obj) => {
					const isInreduce = acc.find(item => item.id === obj.parentId);
					if (isInreduce) {
						isInreduce.attrValue.push({
							activityPrice: obj.activityPrice,
							attrValueId: obj.skuId,
							operateCityId: obj.operateCityId,
							quota: obj.quota,
						});
					} else {
						acc.push({
							sort: obj.sort,
							id: obj.parentId,
							attrValue: [{
								activityPrice: obj.activityPrice,
								attrValueId: obj.skuId,
								operateCityId: obj.operateCityId,
								quota: obj.quota,
							}]
						});
					}
					return acc;
				}, [])
				console.log('newDataValue:', newDataValue)



				this.form.id = this.activityId ? this.activityId : this.form.id;
				this.form.productList = newDataValue;
				addSeckillProApi(this.form)
					.then((res) => {
						uni.showToast({
							title: "提交成功，请前往配置库存",
							icon: "success",
						});
						// if (this.$route.params.activityId) {
						// 	this.$router.push({
						// 		path: `/marketing/seckill/seckillActivity`
						// 	});
						// } else {
						// 	this.$router.push({
						// 		path: `/marketing/seckill/list`
						// 	});
						// }
					})
					.catch((res) => {
						console.log('res:', res)
					});

			},
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	// // #ifndef APP-PLUS */
	// page {
	// 	width: 100%;
	// 	min-height: 100%;
	// 	// display: flex;
	// }

	// /* #endif

	page {
		height: 100%;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.bg_color {
		@include main_bg_color(theme);
	}

	.main_color {
		@include main_color(theme);
	}

	.plain_bgColor {
		@include main_color(theme);
		@include main_bg_rgba_color(theme, 0.05);
		// border: 1rpx solid $main-color-theme1;
		border: 1rpx solid;
	}

	.card {
		margin: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
	}

	.header-top {
		display: flex;
		justify-content: space-between;
	}

	.product_demo {
		display: flex;
	}

	.header_name {
		font-size: 31rpx;
		font-weight: bolder;
	}

	.card {
		margin: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;

		.name-param {
			margin: 16rpx 0;
			display: flex;
			flex-flow: row;
			justify-content: space-between;
		}
	}

	.name-param1 {
		margin: 16rpx 0;
		display: flex;
		flex-flow: row;
		justify-content: space-between;
	}

	// .border-item {
	// 	border-bottom: 1px solid #f3f3f3;
	// }

	.swiper-item {
		flex: 1;
		flex-direction: column;
	}

	.swiper-page {
		flex: 1;
		flex-direction: column;
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
	}

	.tab-view {
		display: flex;
		flex-direction: column;
		flex: 1;
		height: 820rpx;

		.scroll-view {
			height: 100%;
		}
	}

	.sku-select1 {
		.flex-text {
			margin: 8rpx 8rpx;
			background-color: #fff;
			line-height: 62rpx;
			text-align: center;
			color: #777;
			padding: 0 24rpx;
			font-size: 28rpx;
			border-radius: 12rpx;
			border: 2rpx solid #dddddd;
		}

		.right-class-active {
			@include main_color(theme);
			// @include main_bg_rgba_color(theme, 0.05);
			border: 2rpx solid;
		}
	}

	.ensure {
		width: 100%;
		background-color: #fff;
		padding-bottom: 22rpx;
		padding-bottom: calc(22rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
		padding-bottom: calc(22rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/

		.title {
			font-size: 36rpx;
			color: #282828;
			text-align: center;
			margin: 38rpx 0 36rpx 0;
			position: relative;

			.iconfont {
				position: absolute;
				right: 30rpx;
				top: 0;
				font-size: 36rpx;
			}

			.iconfontRight {
				position: absolute;
				right: 30rpx;
				top: 0;
				font-size: 30rpx;
				@include main_color(theme);
			}
		}

		.titleRight {
			font-size: 30rpx;
			font-weight: bold;
			color: #282828;
			text-align: center;
			margin: 20rpx 10rpx 36rpx 0;
			position: relative;
			padding-bottom: 30rpx;
			border-bottom: 2rpx solid #f0edf0;

			.iconfont {
				position: absolute;
				right: 0;
				top: 0;
				font-size: 30rpx;
			}
		}
	}
</style>